package cn.zqh.commodity.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
  * @Author: zqh
  * @Date: 
  * @Description: esconfiguration
*/
@Configuration
public class EsConfig {
    @Value("${elasticsearch.protocol}")
    private String protocol;

    @Value("${elasticsearch.ipAddress}")
    private String ipAddress;

    @Value("${elasticsearch.port}")
    private Integer port;

    @Value("${elasticsearch.user}")
    private String user;

    @Value("${elasticsearch.password}")
    private String password;


    @Bean
    public RestHighLevelClient elasticsearchAlmClient() {
        RestClientBuilder builder = RestClient.builder(
                new HttpHost(ipAddress, port, protocol),
                new HttpHost(ipAddress, port, protocol),
                new HttpHost(ipAddress, port, protocol));
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
        builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
        RestHighLevelClient restClient = new RestHighLevelClient(builder);
        return restClient;
    }



}
